//
//  TMChangePswController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/23.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMChangePswController.h"

@interface TMChangePswController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UITextField *beforeTf;
@property (weak, nonatomic) IBOutlet UITextField *psdNewTf;
@property (weak, nonatomic) IBOutlet UIButton *codebtn;
@property (weak, nonatomic) IBOutlet UITextField *codeTf;

@end

@implementation TMChangePswController
- (IBAction)codeBtnAction:(UIButton *)sender {
    if (self.phoneTf.text.length != 11) {
        [TMProgressHUD showInfoWithStatus:@"请输入正确的电话号码"];
        return;
    }
    [self.codebtn startTimer:60 andKey:@"keys"];
    [TMUserManager verificationCodeSendWithDictionary:self.phoneTf.text];
}

- (IBAction)compelteBtnAction:(UIButton *)sender {
    if (self.phoneTf.text.length != 11) {
        [TMProgressHUD showInfoWithStatus:@"请输入正确的电话号码"];
        return;
    }
//    if (self.beforeTf.text.length <= 0 || !self.beforeTf.text) {
//        [TMProgressHUD showInfoWithStatus:@"旧密码不能为空"];
//        return;
//    }
    if (self.codeTf.text.length <= 0 || !self.codeTf.text) {
        [TMProgressHUD showInfoWithStatus:@"验证码不能为空"];
        return;
    }
//    if (self.psdNewTf.text.length <= 0 || !self.psdNewTf.text) {
//        [TMProgressHUD showInfoWithStatus:@"新密码不能为空"];
//        return;
//    }
    if (![SPToolSet checkPassword:self.beforeTf.text]) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入6-18位字母数字组合密码！"];
        return;
    }
    if (![SPToolSet checkPassword:self.psdNewTf.text]) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入6-18位字母数字组合密码！"];
        return;
    }
    NSDictionary *dic = @{@"uid":@([TMUserManager deaufatManager].userModel.uid),@"token":[TMUserManager deaufatManager].userModel.token,@"phone": self.phoneTf.text,@"pwd": self.beforeTf.text, @"password": self.psdNewTf.text,@"code":self.codeTf.text};
    [TMUserManager changePasswordWithDictionary:dic Success:^(id respon) {
        [TMProgressHUD showSuccessWithStyleDarkStatus:@"修改成功"];
        [TMUserManager deaufatManager].userModel.password = self.psdNewTf.text;
        [[TMUserManager deaufatManager] updateUserModel:[TMUserManager deaufatManager].userModel];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    self.beforeTf.delegate = self;
    self.phoneTf.delegate = self;
    self.psdNewTf.delegate = self;
    self.codeTf.delegate = self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.phoneTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.phoneTf.text.length >= 11) {
            self.phoneTf.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    if (textField == self.beforeTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.beforeTf.text.length >= 18) {
            self.beforeTf.text = [textField.text substringToIndex:18];
            return NO;
        }
    }
    if (textField == self.psdNewTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.psdNewTf.text.length >= 18) {
            self.psdNewTf.text = [textField.text substringToIndex:18];
            return NO;
        }
    }
    if (textField == self.codeTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.codeTf.text.length >= 8) {
            self.codeTf.text = [textField.text substringToIndex:8];
            return NO;
        }
    }
    
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return YES;
}
@end
